//
//  EditSelfTableViewController.m
//  17DianYing
//
//  Created by apple on 15/9/24.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "EditSelfTableViewController.h"
#import "EditSelfManager.h"


@interface EditSelfTableViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *headImager;

@property (weak, nonatomic) IBOutlet UITextField *Name;

@property (weak, nonatomic) IBOutlet UITextField *Age;

@property (weak, nonatomic) IBOutlet UITextField *Gender;

@property (weak, nonatomic) IBOutlet UITextField *PhoneID;

@property (weak, nonatomic) IBOutlet UITextField *WeiXin;

@property (weak, nonatomic) IBOutlet UITextField *Mail;

@property(nonatomic,strong) EditSelfManager *editSelfM;

@property(nonatomic,strong)UIImage *headButtonImage;

@end

@implementation EditSelfTableViewController

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    self.headButtonImage = [[UIImage alloc]init];
    
    self.editSelfM = [EditSelfManager sharManager];
    
    [self getHeadImager];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getHeadImager
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *usertoken = [user objectForKey:@"usertoken"];
    NSString *typstr = @"jpg";
    UIImage *image = self.headButtonImage;
    [self.editSelfM getHeadImagerWithToken:usertoken andImage:image andTyp:typstr andBlock:^(NSDictionary *dic) {
     
        
    }];
}
- (IBAction)tupImageButton:(id)sender
{
    UIActionSheet *sheet;
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
        
    }
    
    else {
        
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
        
    }
    
    sheet.tag = 255;
    
    [sheet showInView:self.view];
}


-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255) {
        
        NSUInteger sourceType = 0;
        
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            switch (buttonIndex) {
                case 0:
                    // 取消
                    return;
                case 1:
                    // 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                    
                case 2:
                    // 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
            }
        }
        else {
            if (buttonIndex == 0) {
                
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
      
        // 跳转到相机或相册页面
      
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        
        imagePickerController.delegate = self;
        
        imagePickerController.allowsEditing = YES;
        
        imagePickerController.sourceType = sourceType;
        
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    
    
    self.headButtonImage = image;
    
    [self.headImager setBackgroundImage:self.headButtonImage forState:0];

    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}



#pragma mark - Table view data source


@end
